var monitorCameraVue = new Vue({
    el:'#monitorCameraVue',
    data:{
        cameraList:[] //摄像头列表
        ,window_id:{}//窗口id对应的播放ip
        ,g_iWndIndex:0 //当前选中的窗口号
        ,streamtype:1//码流类型，主码流
        ,iChannelID:0//虚拟通道
        ,bZeroChannel:true
        ,previewPicPath:"D:/" //默认抓图路径
        ,isOpenDz:false//是否开启电子放大
        ,treeId:''//当前选择的id
        ,tree:{}//当前选择的节点
        ,picUrl:''
    },
    mounted:function () {
        var _this = this;
        layui.use('element', function(){
            var element = layui.element;

            //…
        });
        _this.initMonitorList();
        _this.initMonitorCamera();
    },
    methods: {
        initMonitorList: function () {
            var _this = this;
            var param = {
                'wareHouseId': localStorage.getItem("house")
            };
            $.post("/monitor/bmonitorcamera/lists", param, function (data, textStatus, jqXHR) {
                _this.cameraList = data.monitorCameraList;
                _this.initCameraTree();
            })
        },
        initCameraTree: function () {
            var _this = this;
            var setting = {
                view: {
                    showIcon: showIconForTree
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: onClick
                }
            };
            var zNodes = _this.cameraList;

            function showIconForTree(treeId, treeNode) {
                return !treeNode.isParent;
            };

            function onClick(event, treeId, treeNode, clickFlag) {
                console.log(treeNode);
                var name = treeNode.name;
                var ip = treeNode.ip;
                var username = treeNode.username;
                var password = treeNode.password;
                var port = treeNode.port;
                _this.tree= treeNode;
                //该处进行播放
                _this.cameraPlay(ip,port,username,password );
            }

            $(document).ready(function () {
                $.fn.zTree.init($("#cameraTreeDiv"), setting, zNodes);
            });
        },
        cameraPlay:function(ip,port,username,password){
            var _this = this;
            //-----------------------------停止当前窗口的播放------------------------------------
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);
//            _this.showMSG("当前窗口："+_this.g_iWndIndex)
//            _this.showMSG("当前窗口状态：",oWndInfo)
            if (oWndInfo != null){
                var stopPlay = WebVideoCtrl.I_Stop();
//                _this.showMSG("当前窗口停止：",stopPlay)
                var logout = WebVideoCtrl.I_Logout(_this.window_id[_this.g_iWndIndex]);//登出
//                _this.showMSG("当前窗口退出：",logout)
            }
            //-----------------------------停止当前窗口的播放------------------------------------
            // 登陆当前窗口
            var iRet = WebVideoCtrl.I_Login(ip, 1, port, username, password, {

                success: function (xmlDoc) {
//                	_this.showMSG(ip + " 登录成功！");
                    var startRealPlay = WebVideoCtrl.I_StartRealPlay(ip, {
                        iStreamType: _this.iStreamType,
                        iChannelID: _this.iChannelID,
                        bZeroChannel: _this.bZeroChannel
                    });
//                    _this.showMSG("播放：",startRealPlay)
                    if (0 == startRealPlay) {
                        _this.window_id[_this.g_iWndIndex] = ip;
//                        _this.showMSG("开始预览成功");
                    } else {
                    	_this.showMSG("开始预览失败");
                    }
                },
                error: function () {
                	_this.showMSG(ip + " 登录失败！");
                }
            });
//            _this.showMSG("登录：",iRet)
            // 播放当前窗口

        },
        closeCamera:function(){
        	//关闭摄像头
			var _this = this;
			
			var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);//获取当前窗口u状态

			if (oWndInfo != null) {
				var iRet = WebVideoCtrl.I_Stop();
				if (0 == iRet) {
//					_this.showMSG("停止预览成功！");
					//退出
					var szIP = _this.window_id[_this.g_iWndIndex];
				  	var iRet = WebVideoCtrl.I_Logout(szIP);
				  	if (0 == iRet) {
//				  		_this.showMSG("退出成功！");
					} else {
						_this.showMSG("退出失败！")
					}
				} else {
					_this.showMSG("停止预览失败！");
				}
			}
        },
        getCapturePath:function(){
        	var _this = this;
        	// 打开选择框 0：文件夹  1：文件
        	var iType = 0;
        	var szDirPath = WebVideoCtrl.I_OpenFileDlg(iType);
        	if (szDirPath != -1 && szDirPath != "" && szDirPath != null) {
        		_this.previewPicPath = szDirPath;
        		
        		var arrXml = [];
    	    	arrXml.push("<LocalConfigInfo>");
    	    	arrXml.push("<PackgeSize></PackgeSize>");
    	    	arrXml.push("<PlayWndType></PlayWndType>");
    	    	arrXml.push("<BuffNumberType></BuffNumberType>");
    	    	arrXml.push("<RecordPath></RecordPath>");
    	    	arrXml.push("<CapturePath>" + _this.previewPicPath + "</CapturePath>");
    	    	arrXml.push("<PlaybackFilePath></PlaybackFilePath>");
    	    	arrXml.push("<PlaybackPicPath></PlaybackPicPath>");
    	    	arrXml.push("<DownloadPath></DownloadPath>");
    	    	arrXml.push("<IVSMode></IVSMode>");
    	    	arrXml.push("<CaptureFileFormat></CaptureFileFormat>");
    	        arrXml.push("<ProtocolType></ProtocolType>");
    	    	arrXml.push("</LocalConfigInfo>");
    	
    	    	var iRet = WebVideoCtrl.I_SetLocalCfg(arrXml.join(""));
    	    	if (0 == iRet) {
//    	    		_this.showMSG("本地配置设置成功！");
    	    	} else {
    	    		_this.showMSG("本地配置设置失败！");
    	    	}
        	}
	    },
	    catchPic:function(){
	    	var _this = this;
            $.get(baseURL + "/monitor/bmonitorcamera/catchPic/"+this.tree.id, function(r){
                var date=r.date;
                if(date.flag){
                    layer.open({
                        type: 1,
                        area: ['340px', '215px'],
                        offset: 'rb',
                        shade: [0],
                        time: 10000,
                        content: '<div>抓拍成功,查看地址:<a href="'+date.url+'" target="_blank">'+date.url+'</a></div>'
                    });
                }else{
                    vm.showMSG("抓拍失败，请联系管理员")
                }
            });
	    	/*if(_this.previewPicPath && _this.previewPicPath.length>0){
	    		var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);debugger;
		    	if (oWndInfo != null) {
		    			var szPicName = _this.window_id[_this.g_iWndIndex] + "_" + _this.iChannelID + "_" + new Date().getTime();
		    			var iRet = WebVideoCtrl.I_CapturePic(szPicName);
		    		if (0 == iRet) {
		    			_this.showMSG("抓图成功！"+"路径："+_this.previewPicPath);
		    		} else {
		    			_this.showMSG("抓图失败！");
		    		}
		    	}
	    	}else{
	    		//右下角弹出  请先选
	    		
	    	}*/


	    },
	    dzfd:function(){
	    	var _this = this;
	    	var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);
	    	if(_this.isOpenDz){
		    	if (oWndInfo != null) {
		    		var iRet = WebVideoCtrl.I_DisableEZoom();
		    		if (0 == iRet) {
		    			_this.showMSG("禁用电子放大成功！");
		    		} else {
		    			_this.showMSG("禁用电子放大失败！");
		    		}
		    	}
	    	}else{
	    		_this.isOpenDz = true;
		    	if (oWndInfo != null) {
		    		var iRet = WebVideoCtrl.I_EnableEZoom();
		    		if (0 == iRet) {
		    			_this.showMSG("启用电子放大成功！");
		    		} else {
		    			_this.showMSG("启用电子放大失败！");
		    		}
		    	}
	    	}
	    },
        initMonitorCamera: function () {
            var _this = this;
            // 检查插件是否已经安装过
            if (-1 == WebVideoCtrl.I_CheckPluginInstall()) {
            	_this.showMSG("您还未安装过插件，双击开发包目录里的WebComponents.exe安装！");
                return;
            }

            // 初始化插件参数及插入插件
            var domHeight = $("#leftDiv").width() - 20;
            WebVideoCtrl.I_InitPlugin(domHeight, domHeight * 3 / 5, {
                iWndowType: 2,
                cbSelWnd: function (xmlDoc) {
                    _this.g_iWndIndex = $(xmlDoc).find("SelectWnd").eq(0).text();
                }
            });
            WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin");

            // 检查插件是否最新
            if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
            	_this.showMSG("检测到新的插件版本，双击开发包目录里的WebComponents.exe升级！")
                return;
            }
        },
        changeWndNum:function () {
        	var _this = this;
            var splitWind = $("#splitWind").val();
            WebVideoCtrl.I_ChangeWndNum(splitWind);
        },
        mouseDownPTZControl: function (type) {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex),
                bZeroChannel = $("#channels option").eq($("#channels").get(0).selectedIndex).attr("bZero") == "true" ? true : false,
                iPTZSpeed = $("#ptzspeed").val(),
                bStop = false;

            if (bZeroChannel) {// 零通道不支持云台
                return;
            }

            if (oWndInfo != null) {
                if (9 == iPTZIndex && g_bPTZAuto) {
                    iPTZSpeed = 0;// 自动开启后，速度置为0可以关闭自动
                    bStop = true;
                } else {
                    g_bPTZAuto = false;// 点击其他方向，自动肯定会被关闭
                    bStop = false;
                }

                WebVideoCtrl.I_PTZControl(iPTZIndex, bStop, {
                    iPTZSpeed: iPTZSpeed,
                    success: function (xmlDoc) {
                        if (9 == iPTZIndex) {
                            g_bPTZAuto = !g_bPTZAuto;
                        }
//                        _this.showMSG(oWndInfo.szIP + " 开启云台成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + " 开启云台失败！");
                    }
                });
            }
        },
        mouseUpPTZControl: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);
            
            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(1, true, {
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 停止云台成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + " 停止云台失败！");
                    }
                });
            }
        },
        PTZZoomIn: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);
            console.log(oWndInfo)
            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(10, false, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 变倍+成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  变倍+失败！");
                    }
                });
            }
        },
        PTZZoomout: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(11, false, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 变倍-成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  变倍-失败！");
                    }
                });
            }
        },
        PTZZoomStop: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(11, true, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 变倍停止成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  变倍停止失败！");
                    }
                });
            }
        },
        PTZFocusIn: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(12, false, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 聚焦+成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  聚焦+失败！");
                    }
                });
            }
        },
        PTZFoucusOut: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(13, false, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 聚焦-成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  聚焦-失败！");
                    }
                });
            }
        },
        PTZFoucusStop: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(12, true, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 聚焦停止成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  聚焦停止失败！");
                    }
                });
            }
        },
        PTZIrisIn: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(14, false, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 光圈+成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  光圈+失败！");
                    }
                });
            }
        },
        PTZIrisOut: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(15, false, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 光圈-成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  光圈-失败！");
                    }
                });
            }
        },
        PTZIrisStop: function () {
        	var _this = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(_this.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(14, true, {
                    iWndIndex: _this.g_iWndIndex,
                    success: function (xmlDoc) {
//                    	_this.showMSG(oWndInfo.szIP + " 光圈停止成功！");
                    },
                    error: function () {
                    	_this.showMSG(oWndInfo.szIP + "  光圈停止失败！");
                    }
                });
            }
        },
        showMSG:function(msg){
//        	console.log(msg)
        	layer.open({
        		type: 1, 
        		area: ['340px', '215px'],
        		offset: 'rb',
        		shade: [0],
        		time: 1000,
        		content: '<div style="padding: 20px 100px;">'+msg+'</div>'
        	});
        	
        },
        // 全屏
        clickFullScreen:function () {
        	WebVideoCtrl.I_FullScreen(true);
        }
    }
});